package com.blt.govdata.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.blt.govdata.model.TProjectProgramment;
import com.blt.govdata.service.ITProjectProgrammentService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-09-16
 */
@RestController
@RequestMapping("/tProjectProgramment")
//todo 将所有的XXX修改为真实值
public class TProjectProgrammentController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITProjectProgrammentService service;

    /**
     * 通过项目ID查询
     */
    @RequestMapping(value = "selectByPid", params = {"pId"})
    public Map<String, Object> selectByPid(Integer pId) {
        return ImmutableMap.of("status", "true", "data", this.service.selectList(BeanUtils.instantiate(EntityWrapper.class).where("p_id = {0}", pId)));
    }


    /**
     * 新增
     */
    @RequestMapping(value = "addProgramment", params = {"pId"})
    public Map<String, Object> addProgramment(TProjectProgramment programment) {
        boolean result = this.service.insert(programment);
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 修改
     */

    @RequestMapping(value = "editProgramment", params = {"id"})
    public Map<String, Object> editProgramment(TProjectProgramment programment) {
        boolean result = this.service.updateById(programment);
        return ImmutableMap.of("status", "true", "data", result);
    }


    /**
     * 删除
     */

    @RequestMapping(value = "delete", params = {"id"})
    public Map<String, Object> delete(Integer id) {
        boolean result = this.service.deleteById(id);
        return ImmutableMap.of("status", "true", "data", result);
    }


    /**
     * 新增或修改
     */
    @RequestMapping(value = "createOrModifyProgramment", params = {"pId"})
    public Map<String, Object> createOrModifyAfter(TProjectProgramment programment) {
        TProjectProgramment one = this.service.selectOne(BeanUtils.instantiate(EntityWrapper.class).where("p_id = {0}", programment.getpId()));
        if (null == one) {
            return addProgramment(programment);
        }
        return editProgramment(programment);
    }
}
